// const puppeteer = require("puppeteer");
const puppeteer = require("puppeteer-core");
const fs = require("fs")
const path = require("path")
const pixels = require("image-pixels")
const resemble = require("resemblejs")


const config = {
    username: '18838192613',
    password: '19961120TZZ'
}
// let loginUrl = 'https://hotels.ctrip.com/hotels/list?city=-1&checkin=2023/03/10&checkout=2023/03/11&optionId=11&optionType=Province&directSearch=1&optionName=%E6%B0%91%E5%AE%BF&display=%E6%B0%91%E5%AE%BF&crn=1&adult=1&children=0&searchBoxArg=t&travelPurpose=0&ctm_ref=ix_sb_dl&domestic=1&'
let loginUrl = 'https://hotel.fliggy.com/hotel_list3.htm?_input_charset=utf-8&cityName=%E5%B1%B1%E8%A5%BF&city=&keywords=%E6%B0%91%E5%AE%BF&checkIn=&checkOut=&_output_charset=utf8'

function waitForTimeout(time) {
    return new Promise((resolve, reject) => setTimeout(resolve, time));
}

const run = async () => {
    const browser = await puppeteer.launch({ 
        headless: false,
        args: ['--start-maximized'],
        // executablePath: 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe'
        executablePath: 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
    }) // 如果不需要看到浏览器爬取的过程，将 headless 设置为 true 即可。
    const page = await browser.newPage();
    // 访问页面
    await page.goto(loginUrl);
    await waitForTimeout(10000);

    await page.type('#nloginname', config.username, { delay: 1000 });
    await page.type('#npwd', config.password, { delay: 1100 });
    await page.click(".checkbox-agreement label");
    await waitForTimeout(1000);
    await page.click("#nsubmit");
    return
    
    // 实现一个登录行为
    await page.waitForSelector(".thirdLogin");
    await page.click(".thirdLogin");
    await page.click(".thirdLogin .password");
    await page.waitForSelector(".form-control");
    await page.waitForSelector(".password-login_wrapper .btn-primary.login-btn");

    await page.type('.password-login_wrapper .form-control', config.username, { delay: 10 });
    await page.type('.password-login_wrapper .password-input', config.password, { delay: 10 });

    await waitForTimeout(1000);
    await page.click(".password-login_wrapper .btn-primary.login-btn");

    await waitForTimeout(5000);

    // 获取滑动距离
    // async function getDistance() {
    //     let { bg, fullbg } = await page.evaluate(() => {
    //         const fullbg = document.querySelector(".geetest_canvas_fullbg");
    //         const bg = document.querySelector(".geetest_canvas_bg");
    //         return { bg: bg.toDataURL(), fullbg: fullbg.toDataURL() };
    //     });
    //     bg = bg.replace(/^data:image\/\w+;base64,/, "");
    //     fullbg = fullbg.replace(/^data:image\/\w+;base64,/, "");
    //     var bgDataBuffer = new Buffer(bg, "base64");
    //     var fullbgDataBuffer = new Buffer(fullbg, "base64");
    //     fs.writeFileSync(bgImg, bgDataBuffer); fs.writeFileSync(fullbgImg, fullbgDataBuffer); // 通过resemble比较背景图和缺口图的不同 
    //     resemble(bgImg).compareTo(fullbgImg).ignoreColors().onComplete(async function (data) {
    //         fs.writeFileSync(path.resolve(__dirname, `diff.png`), data.getBuffer());
    //     });
    //     var { data } = await pixels(path.resolve(__dirname, `diff.png`), { cache: false }); // 获取缺口距离左边的做小位置，即计为需要滑动的距离 
    //     let arr = [];
    //     for (let i = 10; i < 150; i++) { for (let j = 80; j < 220; j++) { var p = 260 * i + j; p = p << 2; if (data[p] === 255 && data[p + 1] === 0 && data[p + 2] === 255) { arr.push(j); break; } } }
    //     return Math.min(...arr);
    // }
    // const distance = await getDistance();
    // console.log(distance, 'distance')
    // const button = await page.$(slidetrigger)
    // const box = await button.boundingBox()
    // const axleX = Math.floor(box.x + box.width / 2)
    // const axleY = Math.floor(box.y + box.height / 2)
    // console.log(distance, "distance");
    // console.log(box.x + distance);
    // await btnSlider(distance)
    // async function btnSlider(distance) {
    //     await page.mouse.move(axleX, axleY)
    //     await page.mouse.down()
    //     await page.waitFor(200)
    //     await page.mouse.move(box.x + distance / 4, axleY, { steps: 20 })
    //     await page.waitFor(200)
    //     await page.mouse.move(box.x + distance / 3, axleY, { steps: 18 })
    //     await page.waitFor(350)
    //     await page.mouse.move(box.x + distance / 2, axleY, { steps: 15 }); await page.waitFor(400)
    //     await page.mouse.move(box.x + (distance / 3) * 2, axleY, { steps: 15 }); await page.waitFor(350)
    //     await page.mouse.move(box.x + (distance / 4) * 3, axleY, { steps: 10 }); await page.waitFor(350)
    //     await page.mouse.move(box.x + distance + 20, axleY, { steps: 10 })
    //     await page.waitFor(300)
    //     await page.mouse.up()
    //     await page.waitFor(1000)
    // }
};

run();
function getMoreNum(arr) {
    var obj = {}
    var arr1 = []
    for (var i = 0; i < arr.length; i++) {
        if (arr1.indexOf(arr[i]) == -1) { obj[arr[i]] = 1; arr1.push(arr[i]) }
        else { obj[arr[i]]++; }
    } var max = 0
    var maxStr
    for (var i in obj) { if (max < obj[i]) { max = obj[i]; maxStr = i; } }
    return { max, maxStr }
}

